--
-- Author: shunguo.chen
-- Date: 2018-06-07 11:27:37
--
-- TheatreView 歌剧院操作界面
--

local TheatreBossPanel = import(".TheatreBossPanel")
local LifePanel = import(".LifePanel")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.TheatreScene

function ClassRef:init()
	self._timeText = self._ui["top/clock/time"]

	self._ui.btn_esc:addClickEventListener(function()
		executeFunc(self._onOperate, "escape")
	end)

	self._bossPanel = TheatreBossPanel.create(self._ui.boss)
	self._lifePanel = LifePanel.create(self._ui.life)
	self._lifePanel:initType(2)
end

-- info = TheatreInfo
-- isInBattle = bool 是否在战斗中
function ClassRef:refresh(info, isInBattle)
	self._info = info

	self._bossPanel:setData(info)
	self._lifePanel:setData(info:getLifeData())

	self:setIsInBattle(isInBattle)
end

function ClassRef:setIsInBattle(isInBattle)
	self._timeText:stopAllActions()
	self._ui.top:setVisible(not isInBattle)
	if not isInBattle then
		self._totalTime = self._info:getRestTime()
		self:_refreshTime()
	end
end

-- message SessionResult {
-- 	int32	 sessionID      			= 1;//ID
-- 	bool	 isWon		       			= 2;//是否已完成
-- 	int32    loserBossLeftPercent  		= 3;//失败方Boss残余血量百分比
-- }
function ClassRef:playPunishAction(msg, cb)
	-- 1.输者boss血量减少到0
	self._bossPanel:playPunishAction(msg.sessionID, msg.isWon, msg.loserBossLeftPercent,
	 function()
		-- 2.输者生命数扣除生命数
		self._lifePanel:onActionOver(function()
			cb()
		end)
		self._lifePanel:setData(self._info:getLifeData())
	end)
end

function ClassRef:updateBossHp()
	self._bossPanel:updateBossHp()
end

function ClassRef:updateTeamLife()
	self._lifePanel:setData(self._info:getLifeData())
end

function ClassRef:_refreshTime()
	self._timeText:setString(TimeUtil.secondsToTime(self._totalTime))

	self._totalTime = self._totalTime - 1
	if self._totalTime <= 0 then
		return
	end

	self._timeText:performWithDelay(function()
			self:_refreshTime()
		end, 1)
end

return ClassRef
